//
//  PhotoBrowserAnimator.swift
//  LZFPhotoBrowser
//
//  Created by 梁梓烽 on 16/7/26.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

import UIKit

protocol PresentDelegate :class{
    func homeRect(indexPath:NSIndexPath)->CGRect
    func photoBrowserRect(indexPath:NSIndexPath)->CGRect
    func imageView(indexPath : NSIndexPath) -> UIImageView
}

protocol DismissDeleagte :class{
  
    func currentIndexPath()->NSIndexPath
    func imageView(indexPath : NSIndexPath)->UIImageView
    
}



class PhotoBrowserAnimator: NSObject{

     var isPresented : Bool = false
     var indexPath :NSIndexPath?
    
    weak var presentDelegate :PresentDelegate?
    weak var dismissDelegate :DismissDeleagte?
}




extension PhotoBrowserAnimator : UIViewControllerTransitioningDelegate{
    
      func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresented = true
         return self
    }
  
      func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        
        isPresented = false
        return self
    }
    
}



extension PhotoBrowserAnimator :UIViewControllerAnimatedTransitioning
{
     func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 2.0
    }
  
     func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        
        isPresented ? animateForPresent(transitionContext) : dismissForPresent(transitionContext)
     
    }
 
    
    
    func animateForPresent(transitionContext: UIViewControllerContextTransitioning){
        //拿到即将弹出来的view(控制器的view)
        let presetView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //将要弹出的控制器添加到containerView上
        transitionContext.containerView()?.addSubview(presetView)
        
        
        
        
        /*
        1.要拿到iamgeView的frame, 跳转后imageview的frame ,拿到要来做动画的imageView
        >从homeViewController上去拿,使用协议
        2.让presentView 设置透明度为0, 动画完成后设置透明度为1
        3.containerView默认是Black颜色的,开始设置背景颜色为黑色,动画完成设置为透明色
        */
        
        //添加用来做动画的iamgeView
        let imageView = presentDelegate!.imageView(indexPath!)
        transitionContext.containerView()?.addSubview(imageView)
        imageView.frame = presentDelegate!.homeRect(indexPath!)
        
        
        presetView.alpha=0
        transitionContext.containerView()?.backgroundColor=UIColor.blackColor()
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            //执行动画
            //block中访问类的成员变量的时候,要用self
            imageView.frame = self.presentDelegate!.photoBrowserRect(self.indexPath!)
            }) { (_) -> Void in
                //动画完成后后执行
                presetView.alpha=1
                transitionContext.containerView()?.backgroundColor=UIColor.clearColor()
                imageView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
        
    }
    
    
     func dismissForPresent(transitionContext: UIViewControllerContextTransitioning){
        
        //取出即将消失的view(控制器的view)
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        //移除控制器的view
        dismissView?.removeFromSuperview()
        
        //对用到的可选类型进行解包处理
        guard let dismissDelegate  = dismissDelegate  else  {
            return
        }
        
        guard let presentDelegate  = presentDelegate  else  {
            return
        }
        
        //拿到当前的显示cell的indexPath, 往containerView上添加imageView
        let currentIndexPath  = dismissDelegate.currentIndexPath()
        
        let imageView  =  dismissDelegate.imageView(currentIndexPath)
        transitionContext.containerView()?.addSubview(imageView)
        
        //imageView的起始位置
        imageView.frame = presentDelegate.photoBrowserRect(currentIndexPath)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            //执行动画
            //最终动画位置
            imageView.frame = presentDelegate.homeRect(currentIndexPath)
            
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
                
                //动画执行完,默认系统会吧containerView从父控件中移除, 所以刚才上面的imageView也移除掉
        }
    }
    
}





